<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >添加部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog :title="isEdit? '编辑部门': '添加部门'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false"	>
      <deptDialog :pid="this.curid" :isEdit="isEdit" @success="hSuccess" v-if="showDialog" @close="hclose" :originList="originList"></deptDialog>
    </el-dialog>
  </div>
</template>


<script>
// 导入api
import { getDepartments, delDepartments } from "@/api/departments";
// 导入 数组转树形的方法
import { array2tree } from "@/utils/index";
// 导入子组件
import deptDialog from "./deptDialog.vue";

export default {
  name: "departments",
  // 注册组件
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 对话框显示隐藏
      showDialog: false,
      // 父传子
      curid: "",
      // 父传子 (让子组件知道是编辑还是新增)
      isEdit:false,
      // 传递给子组件 来做表单验证
      originList: [],
    };
  },
  created() {
    // 查询企业的部门列表
    this.loadDepartments();
  },
  methods: {
    // 查询企业的部门列表
    async loadDepartments() {
      const res = await getDepartments();
      console.log("查询企业的部门列表", res);
      res.data.depts.shift();
      this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
      // 调用数组转树形
      this.list = array2tree(res.data.depts, "");
      // console.log("树形", this.list);

    },
    // 添加按钮
    hAdd(id) {
      // 打开弹层盒子
      this.showDialog = true;
      // 作用域插槽传递id
      this.curid = id;
    },
    // 子传父 的事件
    hSuccess() {
      // 关闭弹层
      this.showDialog = false;
      // 更新数据
      this.loadDepartments();
      // false 为 新增操作
      this.isEdit=false
    },
    hEdit(id){
      // 打开弹层
      this.showDialog = true;
      // true 为编辑操作
      this.isEdit=true
      this.curid = id
    },
    // 删除按钮
    hDel(id) {
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 调用api
          await delDepartments(id);
          // 刷新页面
          this.loadDepartments();
        })
        .catch(() => {
          // 用户取消退出
        });
    },
    // 子传父 点击取消按钮 关闭弹层
    hclose(){
      // 关闭弹窗
      this.showDialog = false;
    }
  },
};
</script>